package com.power.project.model.vo;

import cn.hutool.core.lang.tree.Tree;
import com.power.project.model.Ww_user;
import com.power.project.model.dto.UserDepartDTO;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;

import javax.persistence.Column;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author mrzhao
 * @CreateDate 2024/6/26 10:17
 * @Desc TODO
 * @Version 1.0
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class UserBaseVO {

    @Column(nullable = false)
    @ApiModelProperty(value = "账号，成员标识，全局唯一")
    private String userId;

    @ApiModelProperty(value = "用户头像")
    private String avatar;

    @ApiModelProperty(value = "用户名称")
    private String name;

    @ApiModelProperty(value = "用户手机号")
    private String mobile;

    @ApiModelProperty(value = "主部门Id", hidden = true)
    private String mainDepartId;

    @ApiModelProperty(value = "所属部门")
    private String departments;

    @ApiModelProperty(value = "标签")
    private String tags;

    @ApiModelProperty(value = "是否是管理员")
    private Boolean isOwner;

    public UserBaseVO(Ww_user dto) {
        this.userId = dto.getUserid();
        this.name = dto.getName();
        this.mobile = dto.getMobile();
        this.mainDepartId = dto.getMaindepartment();
        this.avatar = dto.getAvatar();
    }

    public void setDepartments(List<Tree<String>> trees, List<UserDepartDTO> departs) {
        if (CollectionUtils.isEmpty(departs)) {
            return;
        }
        // 拼接路径 ，主部门优先展示
        List<String> paths = new ArrayList<>();
        for (Tree<String> tree : trees) {
            for (UserDepartDTO depart : departs) {
                List<CharSequence> parents = tree.getParentsName(depart.getDepartId(), true);
                if (CollectionUtils.isEmpty(parents)) {
                    continue;
                }
                String[] array =  parents.stream().map(CharSequence::toString).collect(Collectors.toList()).toArray(new String[parents.size()]);
                CollectionUtils.reverseArray(array);
                String path = String.join("/", array);
                if (depart.getDepartId().equals(this.mainDepartId)) {
                    paths.add(0, path);
                } else {
                    paths.add(path);
                }
            }
        }

        this.departments = String.join(";", paths);
    }
}
